package com.firmboy.staticdb;

import com.firmboy.threadPool.PoolFactory;
import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 * 测试连接池
 *
 * @author firmboy
 * @create 2018-04-15 下午7:27
 **/
public class DBTest {




    @Test
    public void dbTest() throws InterruptedException {

        //final DBPoolHelper helper = new DBPoolHelper();
        int threadNumber = 100;
        final CountDownLatch countDownLatch = new CountDownLatch(threadNumber);

        final AtomicInteger count = new AtomicInteger();



        Runnable my = new Runnable() {
            @Override
            public void run() {
                System.out.println("当前执行了"+count.getAndAdd(1)+"个线程");
                List<Map<String, Object>> list = DBPoolHelper.getListObject("select count(id) from test_pool", new Object[]{});
                System.out.println(list.size());
                countDownLatch.countDown();

            }
        };


        ThreadPoolExecutor pool = PoolFactory.getPool();

        for (int i = 0;i<threadNumber;i++){
            pool.execute(my);

        }

        countDownLatch.await();

        System.out.println("所有线程完成");


    }

    @Test
    public void queueTest() throws InterruptedException {
        ArrayBlockingQueue queue = new ArrayBlockingQueue(3);

        queue.put(new Object());
        queue.put(new Object());
        queue.put(new Object());

        for (int i =0 ;i<5;i++){
            Object poll = queue.poll();
            if(poll!=null){
                System.out.println("队列中有数据");
            }else{
                System.out.println("队列中已无数据");
            }


        }


    }
}
